from turbogears.feed       import FeedController
from whatwhat.utils        import getRecentChanges, textilize
from whatwhat.model        import Project  
import cherrypy

    
class WhatWhatFeed(FeedController):
    entrys = []
    
    def _feed_item(self, item, title, item_body):
        entry= {}      
        entry["updated"] = item.creation_date
        entry["title"] = title + " " + item.project.name
        entry["published"] = item.creation_date
        entry["author"] = item.creator.displayName
        entry["link"] = cherrypy.config.get('whatwhat.recentchangesurl')
        entry["summary"] = textilize(item_body[:30])
        self.entrys.append(entry)
        entry = {}
    
    def get_feed_data(self):
        self.entrys = []
        all_projects = Project.select("parent_project_id is NULL order by upper(name)")
        projects = [project for project in all_projects if not project.archived]
        print self._timeframe()
        recent_proj = getRecentChanges(projects, self._timeframe())
        
        for proj in recent_proj:
            self.entry = {}
            if len(proj.recent_issues) > 0:
                for issue in proj.recent_issues:
                    self._feed_item(issue, "Issue:", issue.issue)
                             
            if len(proj.recent_risks) > 0:
                 for risk in proj.recent_risks:
                    self._feed_item(risk, "risk:", risk.description)
                                         
            if len(proj.recent_notes) > 0:
                for note in proj.recent_notes:
                    self._feed_item(note, "note:", note.note)

            if len(proj.recent_questions) > 0:
                for question in proj.recent_questions:
                    self._feed_item(question, "Question:", question.question)
                    if len(question.answers) > 0:
                        for answer in question.answers: 
                            entry = {}
                            entry["updated"] = answer.creation_date
                            entry["title"] = "Question:Answer %s:%s" % (question.question[:30], question.project.name)
                            entry["published"] = answer.creation_date
                            entry["author"] = answer.creator.displayName
                            entry["link"] = cherrypy.config.get('whatwhat.recentchangesurl')
                            entry["summary"] = textilize(answer.answer[:30])
                            self.entrys.append(entry)
                            entry = {}
        
        return dict(
                    title="whatwhat recent changes",
                    link="http://10.0.0.1/dashboard",
                    author={ "name" : "Your Name Here", "email" : "YourEmail@email.com" },
                    id="http://10.0.0.1/dashboard",
                    subtitle="recent changes feed",
                    entrys=self.entrys
               )
               

    def _timeframe(self):
            if cherrypy.request.simpleCookie.has_key('time_frame_id'):
                return int(cherrypy.request.simpleCookie['time_frame_id'].value)
            return 24
